﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Library.Common;
using Library.Bll;
namespace HanhuaMicroCredit.BLL
{
    public class menuBLL
    {
        private const string _menu = "Sys_IBKMmenuTree";//菜单表
        private const string _knowledge = "FAQTree";//知识库表
        //权限角色
        private static string RoleId
        {
            get { return Utils.StrToInt(Current.GetUserInfo("Roleid"), 0).ToString(); }
        }
        private static string ClassId
        {
            get { return "1"; }
        }
        //菜单的第一个id
        private static string Id
        {
            get { return "1"; }
        }
        /// <summary>
        /// 获取子节点菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        private static List<Dictionary<string, object>> getMenuListById(string id, string tableName)
        {
            string selectFields = "id,title,summary";
            StringBuilder conditions = new StringBuilder();
            conditions.AppendFormat(" isvalid = 1 and parid={0} ", Utils.StrToInt(id, 0).ToString());
            //权限
            conditions.AppendFormat(" and id in (select moduleid from Sys_IBPermission where IsValid=1 and Classid={0} and Roleid={1} and IBBigClss = '{2}') ", ClassId, RoleId, tableName);
            
            string orderFields = "[order]";
            return CommonBLL.GetList(tableName, selectFields, orderFields, conditions.ToString());
        }
        /// <summary>
        /// 获取子节点菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        private static List<Dictionary<string, object>> getMenuListById(string id, string tableName, bool b)
        {
            string selectFields = "id,title,summary";
            StringBuilder conditions = new StringBuilder();
            conditions.AppendFormat(" isvalid = 1 and parid={0} ", Utils.StrToInt(id, 0).ToString());
            //权限
            if(b)
            conditions.AppendFormat(" and id in (select moduleid from Sys_IBPermission where IsValid=1 and Classid={0} and Roleid={1} and IBBigClss = '{2}') ", ClassId, RoleId, tableName);
            string orderFields = "[order]";
            return CommonBLL.GetList(tableName, selectFields, orderFields, conditions.ToString());
        }
        /// <summary>
        /// 获取默认菜单
        /// </summary>
        /// <returns></returns>
        public static string getFirstMenu()
        {
            StringBuilder sbHtml = new StringBuilder();
            sbHtml.Append("<ul>");
            List<Dictionary<string, object>> list = getMenuListById(Id, _menu);
            int i = 0;
            foreach (Dictionary<string, object> d in list)
            {
                if (i == 0)
                    sbHtml.Append("<li class=\"selected\">");
                else
                    sbHtml.Append("<li>");
                sbHtml.Append("<a href=\"/Home/LeftMenu/" + d["id"] + "\"><span>" + d["title"] + "</span></a></li>");
                i++;
            }
            sbHtml.Append("</ul>");
            return sbHtml.ToString();
        }
        /// <summary>
        /// 获取子节点菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string getMenuById(string id)
        {
            StringBuilder sbHtml = new StringBuilder();
            sbHtml.Append("<div class=\"accordion\" fillSpace=\"sidebar\">");
            List<Dictionary<string, object>> list = getMenuListById(id, _menu);
            foreach (Dictionary<string, object> d in list)
            {
                sbHtml.Append("  <div class=\"accordionHeader\">");
                sbHtml.Append("    <h2><span>Folder</span>" + d["title"] + "</h2>");
                sbHtml.Append("  </div>");
                sbHtml.Append("  <div class=\"accordionContent\">");
                if (d["id"].ToString() == "131") //知识库
                    sbHtml.Append(getChildMenuById("0", 0, _knowledge, true));
                else
                    sbHtml.Append(getChildMenuById(d["id"].ToString(), 0, _menu, true));
                sbHtml.Append("  </div>");
            }
            sbHtml.Append("  </div>");
            return sbHtml.ToString();
        }
        /// <summary>
        /// 获取子节点菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string getMenuById(string id, string name)
        {
            StringBuilder sbHtml = new StringBuilder();
            sbHtml.Append("<div class=\"accordion\" fillSpace=\"sidebar\">");
            List<Dictionary<string, object>> list = getMenuListById(id, name, false);
            foreach (Dictionary<string, object> d in list)
            {
                sbHtml.Append("  <div class=\"accordionHeader\">");
                sbHtml.Append("    <h2><span>Folder</span>" + d["title"] + "</h2>");
                sbHtml.Append("  </div>");
                sbHtml.Append("  <div class=\"accordionContent\">");
                sbHtml.Append(getChildMenuById(d["id"].ToString(), 0, name, false));
                sbHtml.Append("  </div>");
            }
            sbHtml.Append("  </div>");
            return sbHtml.ToString();
        }
        /// <summary>
        /// 获取默认菜单的第一个id
        /// </summary>
        /// <returns></returns>
        public static string getFirst()
        {
            List<Dictionary<string, object>> list = getMenuListById(Id, _menu);
            if (list.Count > 0)
            {
                return list[0]["id"].ToString();
            }
            return "0";
        }

        /// <summary>
        /// 遍历所有子节点菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="i"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static string getChildMenuById(string id, int i, string tableName, bool b)
        {
            StringBuilder sbHtml = new StringBuilder();
            List<Dictionary<string, object>> list = getMenuListById(id, tableName, b);
            if (list.Count > 0)
            {
                if (i == 0)
                    sbHtml.Append("    <ul class=\"tree treeFolder\">");
                else
                    sbHtml.Append("    <ul>");
                foreach (Dictionary<string, object> d in list)
                {
                    string url = d["summary"].ToString();
                    string ids = d["id"].ToString();
                    string name = d["title"].ToString();

                    sbHtml.Append("       <li><a href=\"" + url + "\" target=\"navTab\" external=\"true\" rel=\"" + ids + "\">" + name + "</a>");
                    sbHtml.Append(getChildMenuById(ids, 1, tableName, b));
                    sbHtml.Append("       </li>");
                }
                sbHtml.Append("    </ul>");
            }
            return sbHtml.ToString();
        }

        public static string getDestChildMenuById(string id, string tableName)
        {
            return getDestChildMenuById(id, 0, tableName, false);
        }

        /// <summary>
        /// 目的地
        /// </summary>
        /// <param name="id"></param>
        /// <param name="i"></param>
        /// <param name="tableName"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static string getDestChildMenuById(string id, int i, string tableName, bool b)
        {
            StringBuilder sbHtml = new StringBuilder();
            List<Dictionary<string, object>> list = getMenuListById(id, tableName, b);
            if (list.Count > 0)
            {
                if (i == 0)
                    sbHtml.Append("    <ul class=\"tree treeFolder treeEdit\" target=\"treeEdit" + id + "\">");
                else
                    sbHtml.Append("    <ul>");
                foreach (Dictionary<string, object> d in list)
                {
                    string ids = d["id"].ToString();
                    string name = d["title"].ToString();
                    //string editHref = "/dest/edit/" + ids; //d["summary"].ToString();
                    //string delHref = "/OperateTotal/Delete?ids=" + ids + "&name=" + DencryptHelper.Encrypt("Dest_Destination");

                    sbHtml.Append("       <li><a href=\"\" target=\"navTab\" onclick=\"setTreeId(" + ids + ")\" external=\"true\" rel=\"" + ids + "\">" + name + "</a>");
                    sbHtml.Append(getDestChildMenuById(ids, 1, tableName, b));
                    sbHtml.Append("       </li>");
                }
                sbHtml.Append("    </ul>");
            }
            return sbHtml.ToString();
        }


        #region tn_2012.2.16 添加数据库参数
        /// <summary>
        /// 获取子节点菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string getMenuById(Database db, string id, string tableName)
        {
            return getChildMenuById(db, id, 0, tableName, false);
        }

        /// <summary>
        /// 获取子节点菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        private static List<Dictionary<string, object>> getMenuListById(Database db, string id, string tableName, bool b)
        {
            string selectFields = "parid,id,title,summary";
            StringBuilder conditions = new StringBuilder();
            conditions.AppendFormat(" isvalid = 1 and parid={0} ", Utils.StrToInt(id, 0).ToString());
            //权限
            //if(b)
            //conditions.AppendFormat(" and id in (select moduleid from Sys_IBPermission where Classid={0} and Roleid={1} and IBBigClss = '{2}') ", ClassId, RoleId, tableName);
            string orderFields = "[order]";
            return CommonBLL.GetList(db, tableName, selectFields, orderFields, conditions.ToString());
        }
        /// <summary>
        /// 遍历所有子节点菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="i"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static string getChildMenuById(Database db, string id, int i, string tableName, bool b)
        {
            StringBuilder sbHtml = new StringBuilder();
            List<Dictionary<string, object>> list = getMenuListById(db, id, tableName, b);
            if (list.Count > 0)
            {
                if (i == 0)
                    sbHtml.Append("    <ul class=\"tree treeFolder treeEdit\" target=\"treeEdit" + id + "\">");
                else
                    sbHtml.Append("    <ul>");
                foreach (Dictionary<string, object> d in list)
                {
                    string ids = d["id"].ToString();
                    string name = d["title"].ToString();
                    //string editHref = "/dest/edit/" + ids; //d["summary"].ToString();
                    //string delHref = "/OperateTotal/Delete?ids=" + ids + "&name=" + DencryptHelper.Encrypt("Dest_Destination");
                    string summary = d["summary"].ToString();
                   // if (summary.IndexOf("keyword") >= 0)
                    if (summary.Length >= 0)
                    {
                        sbHtml.Append(string.Format("       <li><a href=\"\" target=\"navTab\" onclick=\"setTreeId({0},'{1}')\" external=\"true\" rel=\"{2}\">{3}</a>", ids, summary, ids, name));
                    }
                    else
                    {
                        sbHtml.Append("       <li><a href=\"\" target=\"navTab\" onclick=\"setTreeId(" + ids + ",'')\" external=\"true\" rel=\"" + ids + "\">" + name + "</a>");
                    }
                    sbHtml.Append(getChildMenuById(db, ids, 1, tableName, b));
                    sbHtml.Append("       </li>");
                }
                sbHtml.Append("    </ul>");
            }
            return sbHtml.ToString();
        }
        //}
        #endregion
    }
}
